package engine.logic.login
{
	import engine.data.ModelLocator;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.com.config.StaticIni;

	public class ServerSelectUI implements ILoginUI
	{
		public function ServerSelectUI(clip:MovieClip,container:MovieClip)
		{
			_serverList=clip;
			_container=container;
		}
		
		/**登录列表*/
		private var _serverList:MovieClip;
		private var _container:MovieClip;
		//
		private var _completeCallBack:Function; 
		
		//======================界面控制 Start============================
		
		public function show(info:Object=null):void
		{
			return;
			_container.addChild(_serverList);
			
			var severItem:MovieClip;
			
			for(var i:uint = 1 ; i <= 10 ; i ++)
			{
				severItem	=	_serverList.getChildByName("s"+i) as MovieClip;
				severItem.gotoAndStop(1);
				
				severItem.buttonMode		=	true;
				TextField(severItem.t).mouseEnabled=false;
				
				severItem.addEventListener(MouseEvent.ROLL_OVER	,serverListItemRollOverHandler);
				severItem.addEventListener(MouseEvent.ROLL_OUT	,serverListItemRollOutHandler);
				severItem.addEventListener(MouseEvent.CLICK		,serverListItemClickHandler);
				
				var severName:String=ModelLocator.getInstance().serverIni.getContent("server_"+i,"servername");
				
				if(severName)
				{
					TextField(severItem.t).text=severName;
				}
				else
				{
					TextField(severItem.t).text	=	"";
					severItem.gotoAndStop(1);
					severItem.mouseEnabled	=	false;
				}
			}
			
			_serverList.getChildByName("enter").addEventListener(MouseEvent.CLICK,confirmServerHandler);
		}
		
		public function recycle():void
		{
		}
		
		public function hide():void
		{
			_serverList.parent.removeChild(_serverList);
			
			var severItem:MovieClip;
			
			for(var i:uint = 1 ; i <= 10 ; i ++)
			{
				severItem	=	_serverList.getChildByName("s"+i) as MovieClip;
				severItem.gotoAndStop(1);
				
				if(severItem.ip!= "" && severItem.ip != null)
				{
					severItem.buttonMode		=	true;
					severItem.removeEventListener(MouseEvent.ROLL_OVER	,serverListItemRollOverHandler);
					severItem.removeEventListener(MouseEvent.ROLL_OUT	,serverListItemRollOutHandler);
					severItem.removeEventListener(MouseEvent.CLICK		,serverListItemClickHandler);
				}
			}
			
			_serverList.getChildByName("enter").removeEventListener(MouseEvent.CLICK,confirmServerHandler);
		}
		
		
				
		/**从本地取出服务器列表*/
		private function getServerInfo():Object
		{
			var serverItem:MovieClip;
			var serverId:int=-1;
			var serverObj:Object=new Object();
			
			for(var i:uint=1;i<=10;i++)
			{
				serverItem=_serverList.getChildByName("s"+i) as MovieClip;
				if(serverItem.selected)
				{
					serverId=i;
					break;
				}
			}
			
			if(serverId>-1)
			{
				var ip:String=ModelLocator.getInstance().serverIni.getContent("server_"+serverId,"ip");
				var port:String=ModelLocator.getInstance().serverIni.getContent("server_"+serverId,"port");
//				serverObj={ip:ip,port:port};
				serverObj.serverId=ModelLocator.getInstance().serverIni.getContent("server_"+serverId,"serverid");
				serverObj.ip=ip;
				serverObj.port=port;
				
				LoginLogic.ServerID	=	serverObj.serverId;
			}
			serverObj.target=this;
			
			return	serverObj; 	
		}
		
		private function serverListItemRollOverHandler(e:MouseEvent):void
		{
			MovieClip(e.currentTarget).gotoAndStop(2);
		}
		
		private function serverListItemRollOutHandler(e:MouseEvent):void
		{
			var item:MovieClip=e.currentTarget as MovieClip;
			
			if(!item.selected)
			{
				item.gotoAndStop(1);
			}
		}
		
		private function serverListItemClickHandler(e:MouseEvent):void
		{
			var item:MovieClip=e.currentTarget as MovieClip;
			
			var tempItem:MovieClip;
			
			for(var i:uint=1;i<=10;i++)
			{
				tempItem=MovieClip(_serverList.getChildByName("s"+i));
				tempItem.selected=false;
				tempItem.gotoAndStop(1);
			}
			
			item.selected=true;
			item.gotoAndStop(2);
		}
		
		private function confirmServerHandler(e:MouseEvent):void
		{
			var serverInfo:Object=getServerInfo();
			
			if(null		!=	_completeCallBack)
			{
				_completeCallBack(serverInfo);
			}
			
		}
		
		
		public function set completeCallBack(cb:Function):void
		{
			this._completeCallBack=cb;
		}
	}
}